from operator import contains
import math
import uuid
import sys
sys.path.append("/java_web/cloudcs2mon/pyspace/")
import os
os.chdir(os.path.dirname(__file__))
print("1",os.path.dirname(__file__))

import logs
import confhelper
from playwright.sync_api import Playwright, sync_playwright

import dbmapper
from entity import LinkMonResult, MonLinkBatch, MonLinkVersion, MonLink

logger = logs.get_logger('tmall')

wiew_size = {"width": 1380, "height": 2240}
linkpath_prefix = confhelper.confdata().get("linkpath_prefix")+"tmall/"
work_dir = confhelper.confdata().get("work_dir")
urls = []
monLinks = []

picList = []
linkSet = set()

logger.info("sys.argv: {}".format(sys.argv))
version = sys.argv[1]
def run(playwright: Playwright) -> None:
    browser = playwright.firefox.launch(headless=False,slow_mo=3000)
    #context = browser.new_context(screen=wiew_size,viewport=wiew_size,storage_state="tb_state.json")
    context = browser.new_context(screen=wiew_size,viewport=wiew_size,storage_state="tb_state.json")
    # Open new page
    page = context.new_page()
    logger.info("天猫网站爬取数量:{}".format(len(monLinks)))
    i = 0
    hasCompletedUrlCount = 0
    for link in monLinks:
        hasCompletedUrlCount = hasCompletedUrlCount +1
        url = link.url
        linkId = link.id
        linkSet.clear()
        logger.info("清空linkSet:{}".format(len(linkSet)))
        logger.info("开始遍历 url:{} id:{}, 链接整体进度：{}/{}".format(url,linkId,hasCompletedUrlCount,len(monLinks)))
        page.goto(url)

        if page.locator("没有找到相应的店铺信息").count() > 0:
            logger.error("无效店铺: {}".format(url))
            continue
        if page.locator(".J_giftClose.d-close").count()>0:
            logger.info("出现领券框")
            page.locator(".J_giftClose.d-close").click()

        while page.locator(".captcha-tips").count() > 0:
            logger.info("全部宝贝 出现图形验证等待5秒")
            page.wait_for_timeout(5000)

        all_flag = 0
        all_flag = page.locator("text=所有商品").count()
        if all_flag > 0:
            page.locator("text=所有商品").click()
        else:
            logger.error("【所有商品】关键词未匹配，需加入: {}".format(url))
            continue
        try:
            all_flag = page.locator(".banner-box").locator("area:nth-child(2)").wait_for(timeout=10).first.get_attribute("href").count()
            if all_flag > 0:
                page.locator(".banner-box").locator("area:nth-child(2)").first.click()
        except Exception as e:
            print(e)

        logger.info(page.locator(".J_TFilter").locator("b.ui-page-s-len").inner_text())
        page_num_str = page.locator(".J_TFilter").locator("b.ui-page-s-len").inner_text()
        pageNum = 0
        if contains(page_num_str, "/"):
            pageNum = int(page_num_str.split("/")[1])
        print(pageNum)

        logger.info("url {} pagenum: {} ".format(url,pageNum))

        for p_num in range(1,pageNum+1):
            logger.info("进度：url :{} linkId :{} pagenum: {}/{} ".format(url, linkId, p_num, pageNum))
            #page.reload();
            if(p_num > 1):
                page.locator("text=下一页").click()
            pageLinks = page.locator(".J_TItems .item .detail .J_TGoldData").all()
            logger.info("获取到的链接数：{}".format(len(pageLinks)))
            if len(pageLinks) > 0 :
                view_page(p_num,i,page,pageLinks,picList,url,linkId)

        logger.info("结束遍历 {}".format(url))

        monLinkBatch = MonLinkBatch(linkId=linkId)
        linkBatchCount = dbmapper.MonLinkBatchMapper.count(monLinkBatch)
        if int(linkBatchCount) > 0:
            dbmapper.MonLinkBatchMapper.update(MonLinkBatch(goodSum=len(linkSet), linkId=linkId))
        else:
            monLinkBatchInsert = MonLinkBatch(goodSum=len(linkSet), linkId=linkId, status='0', linkUrl=url)
            dbmapper.MonLinkBatchMapper.insert(monLinkBatchInsert)

        monLinkUpdate=MonLink(version=version, url=url)
        dbmapper.MonLinkMapper.update(monLinkUpdate)
    # Close page
    page.close()
    # ---------------------
    context.close()
    browser.close()

def view_page(p_num:int,i:int,page,pageLinks,picList,linkUrl,linkId):
    for p in pageLinks:
        i = i + 1
        j = 0
        try:
            page.wait_for_load_state('networkidle')
            with page.expect_popup(timeout=60000) as popup_info:
                p.first.click()
                logger.info("enabled: {}, visible: {}".format(p.is_enabled(),p.is_visible()))
            page1 = popup_info.value

            while page1.locator(".captcha-tips").count() >0:
                logger.info("出现图形验证等待5秒")
                page1.wait_for_timeout(5000)
            if page1.locator("text=宝贝详情").count() > 0 :
                if page1.locator(".captcha-tips").count() == 0:
                    if page1.url not in linkSet:
                        pic = linkpath_prefix +str(p_num)+"-"+str(i)+"-"+str(j)+page1.title().replace("*","").replace("/","#").replace(" ","")[0:30]+".png"
                        page1.screenshot(path=pic,full_page=True)
                        picList.append(pic)
                        linkMonResult = LinkMonResult(str(uuid.uuid4().int),linkUrl,page1.url,"/profile"+pic.replace(work_dir,""),linkId=linkId)
                        count = dbmapper.LinkMonResultMapper.count(linkMonResult)
                        if int(count) > 0:
                            linkMonResult.status=0
                            linkMonResult.ocrStr=""
                            dbmapper.LinkMonResultMapper.update(linkMonResult)
                        else:
                            dbmapper.LinkMonResultMapper.insert(linkMonResult)

                        linkVersion=MonLinkVersion(linkId=linkId, linkUrl=linkUrl, goodUrl=page1.url,version=version)
                        lvCount = dbmapper.MonLinkVersionMapper.count(linkVersion);
                        if int(lvCount) <= 0:
                            dbmapper.MonLinkVersionMapper.insert(linkVersion)

                        linkSet.add(page1.url)
                        logger.info("商品链接加入集合：{}".format(len(linkSet)))
                    else:
                        logger.info("商品链接已爬取：{}".format(page1.url))
                else:
                    logger.info("出现图形验证进入60秒睡眠")
                    page1.wait_for_timeout(60000)

            page1.close()
        except Exception as e:
            logger.info("exception {} url:{}".format(i,page1.url))
            logger.exception("exception traceback is:{}".format(e))
            page1.close()
            page.reload()

os.chdir(os.path.dirname(__file__))
# 通过db加载url
monLink = dbmapper.MonLinkMapper.list_by_typea('tm', version)
if len(monLink) > 0:
    monLinks = monLink

logger.info("待监测的url %s",[(link.id,link.url) for link in monLinks])
if len(monLinks) > 0:
    with sync_playwright() as playwright:
        run(playwright)
